<?php 

namespace Index\Controller;
use Think\Controller;
use Think\Verify;

class LoginController extends Controller
{
	//主页
	public function indexAction()
	{
		if (isset($_SESSION['username'])) {
			$this->success('你已经登录',U('index/index/index'));
		}else{
			$this->display();
		}
	}
	// 登录
	public function loginAction()
	{
		if (IS_POST) {
			$email=I('post.email','');
			$password=I('post.password','');
			$sql="select * from user where email='{$email}' and password='{$password}'";
			// var_dump($sql);exit;

			$res=M()->query($sql);


			if ($res) {
				$_SESSION['username']=$res[0]['username'];
				$_SESSION['password']=$res[0]['password'];
				$_SESSION['email']=$res[0]['email'];
				$_SESSION['id']=$res[0]['id'];
				$_SESSION['face']=$res[0]['face'];

				$this->success('登录成功',U('index/index/index'));
			}else{
				$this->error('用户名或密码不正确',U('index/index/index'));
			}
		}
	}
	// 自动生成验证码
	public function captchaAction($value='')
	{
		$config=array(
			'fontSize'=>35, //验证码字体大小
			'length'=>  4,  //验证码数量
			);
		$Verify=new Verify($config);
		$Verify->entry();
	}
	// 检测验证码
	// public function checkcaptchaAction($value='')
	// {
	// 	if (IS_AJAX) {
	// 		//I方法，接数据
	// 		$code = I('post.captcha','');
	// 		$Verify= new \Think\Verify();
	// 		if ($Verify->check($code)) {
	// 			echo 1;
	// 		}else{
	// 			echo 0;
	// 		}
	// 	}
	// }
	// 检测邮箱
	public function checkemailAction()
	{
		if (IS_AJAX) {
			$email=I('post.email','');
			$sql="select * from user where email = '{$email}'";
			$res=M()->query($sql);
			if (!$res) {
				$arr=array(
					'error'=>'0',
					'info'=>'该邮箱不存在，请验证后登录'
					);
				echo json_encode($arr);
			}else{
				$arr=array(
					'error'=>'1',
					'info'=>'验证通过'
					);
				echo json_encode($arr);
			}
		}
	}

	//检测密码
	public function checkpasswordAction(){
		if (IS_AJAX) {
			$password=I('post.password','');
			$sql="select * from user where password = '{$password}'";
			$res=M()->query($sql);
			if (!$res) {
				$arr=array(
					'error'=>'0',
					'info'=>'密码错误，请重新输入'
					);
				echo json_encode($arr);
			}else{
				$arr=array(
					'error'=>'1',
					'info'=>'验证通过'
					);
				echo json_encode($arr);
			}
		}
	}

	// 退出
	public function login_outAction()
	{
		session_unset();
		session_destroy();
		$this->success('退出成功',U('index/index/index'));
	}

	
}


 ?>